package com.ebo.learn_demo

import android.os.*
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.helper.widget.Layer
import kotlinx.coroutines.*
import kotlinx.coroutines.android.asCoroutineDispatcher
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.sync.Mutex
import kotlin.system.measureTimeMillis

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        val mLayer: Layer = findViewById(R.id.layer)
//        val animators = listOf<ObjectAnimator>(
//            ObjectAnimator.ofFloat(mLayer, "scaleY", 1f, 2f, 1f),
//            ObjectAnimator.ofFloat(mLayer, "scaleX", 1f, 2f, 1f),
//        )
//        val handPointAnimatorSet = AnimatorSet()
//        handPointAnimatorSet.playTogether(animators)
////            handPointAnimatorSet.interpolator = AnticipateOvershootInterpolator()
//        handPointAnimatorSet.setDuration(1500).start()
//        mLayer.setOnClickListener {
//            val animator = ValueAnimator.ofFloat(0f, 360f)
//            animator.repeatMode = ValueAnimator.RESTART
//            animator.duration = 2000
//            animator.interpolator = LinearInterpolator()
//            animator.repeatCount = ValueAnimator.INFINITE
//            animator.addUpdateListener {
//                mLayer.rotation = it.animatedValue as Float
//            }
//            animator.start()
//        }

        val handler = object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                Log.e("TAG", "handleMessage: ${msg.`when` - SystemClock.uptimeMillis()}")
            }

            override fun dispatchMessage(msg: Message) {
                Log.e("TAG", "dispatchMessage: ${msg.`when` - SystemClock.uptimeMillis()}")
//                super.dispatchMessage(msg)
                msg.callback.run()
            }

            override fun sendMessageAtTime(msg: Message, uptimeMillis: Long): Boolean {
                Log.e("TAG", "sendMessageAtTime: ${uptimeMillis - SystemClock.uptimeMillis()}")
                return super.sendMessageAtTime(msg, uptimeMillis)
            }
        }
        val asCoroutineDispatcher = handler.asCoroutineDispatcher("Coroutine")
        val coroutineScope = CoroutineScope(asCoroutineDispatcher)
        findViewById<TextView>(R.id.tv1).setOnClickListener {
            coroutineScope.launch(asCoroutineDispatcher) {
//
                val stackTrace0 = Looper.myLooper()?.thread?.stackTrace
                val message0 = stackTrace0?.joinToString("\r\n")
                Log.e("TAG", "我是launch:${message0}")
//
//                delay(1000)
//                val stackTrace = Looper.myLooper()?.thread?.stackTrace
//                val message = stackTrace?.joinToString("\r\n")
//                Log.e("TAG", "我是方法一:${message}")
//                delay(1000)
//                val stackTrace2 = Looper.myLooper()?.thread?.stackTrace
//                val message2 = stackTrace2?.joinToString("\r\n")
//                Log.e("TAG", "我是方法二:$message2")
//                Log.e("TAG", "我是方法二")
//                Log.e("TAG", "我是方法二")
//                delay(1000)
//                val stackTrace3 = Looper.myLooper()?.thread?.stackTrace
//                val message3 = stackTrace3?.joinToString("\r\n")
//                Log.e("TAG", "我是方法三:$message3")
//                Log.e("TAG", "我是方法三")
            }
        }


//        mLayer.visibility = View.GONE
    }
}